<?
	/* Twelveseconds - stream reader plugin */	

	// return type of service
	function twelveseconds_getType() { return "media"; }


	// extra the service- action  line ("ike: "I tweeted on twitter.com")
	function twelveseconds_getAction($line) {
		$entry=explode("|",$line);
		
		$entry[7]=trim($entry[7]);
		return "<a href='?service=twelveseconds'><img class='icon' src='".serviceIcon("twelveseconds")."'/></a> Posted a 12 second movie at <a href='http://12seconds.tv'>12seconds.tv</a>";
	}

	// extract and return the correct caption
	function twelveseconds_getCaption($line) {
		$entry=explode("|",$line);
		return $entry[6];
	}

	// extract and return the correct post
	function twelveseconds_getPost($line) {
		$entry=explode("|",$line);
		$itemPost='<iframe src="http://embed.12seconds.tv/i/embed?v='.$entry[3].'" scrolling="no" allowtransparency="true" frameborder="0" width="430" height="360"></iframe>';

		return $itemPost;
	}

	// extract and return the correct link to the item
	function twelveseconds_getLink($line) {
		$entry=explode("|",$line);
		return ""; //'http://12seconds.tv/channel/'.$entry[3];
	}


	// display information about us with a link to the profile
	//  -- this is called to display list of services that are linked (admin-functions.php)
	function twelveseconds_identify($item) {
		$entry=explode("|",$item);
		
		return "<a href='".$entry[3]."'>".$entry[2]." 12 second video</a>";
	}

	

	// get service + profile information
	function twelveseconds_addProfile() {
			$serviceID=$_POST['addservice'];
			$username=$_POST['username'];
	
			$rsslink="http://12seconds.tv/channel/".$username."/feed";
			writeService($serviceID,$username,$rsslink);
	}

	
	function twelveseconds_download($serviceid,$filename)  {
		$reader = new XMLReader();
		$reader->open($filename);
		
		$username=substr($filename,strpos($filename,"channel/")+8);
		$username=substr($username,0,strpos($username,"/feed"));

		set_time_limit(30);
		$count=0;
		
		$itemids=array();
		$continueParsing=true;

		while ($reader->read() && $continueParsing) {
				if ($reader->nodeType==XMLREADER::ELEMENT) {
				
					if ($reader->localName=="item") {
						$guid=null;
						$title=null;
						$mediaurl=null;
						$thumb=null;
						$link=null;
						$pubdate=null;
						$upddate=null;
						
						while ($reader->read()) {

							if ($reader->localName=="link" && !$link) {
								$reader->read();
								$link=$reader->value;
								$guid=$reader->value;
								// grab movie id
								$mediaurl=substr($link,strpos($link,$username)+strlen($username)+1);
								//print $mediaurl."<br/>";
							}
							
							
							if ($reader->localName=="pubDate" && !$pubdate) {
								$reader->read();
								$pubdate=$reader->value;
							}

							if ($reader->name=="title" && !$title) {
								$reader->read();
								$title=$reader->value;
							}
		
							
							if ($reader->localName=="item") {
								if ($guid) {
								
									$indx=date("YmdHis",strtotime($pubdate));
									/*
									if (strpos($filename,"uploads")) {
									} else {
										$indx=date("YmdHis",strtotime($upddate));
									}
									*/
									
									if (newGuid($guid)) {
										$item[$indx]->title=$title;
										$item[$indx]->post=$mediaurl;
										$item[$indx]->link=$link;
										$item[$indx]->guid=$guid;
										$item[$indx]->extra="|".$link."|".$title."|";
										$item[$indx]->pubDate=$pubdate;

										if (strpos($filename,"videos")) {
											$item[$indx]->extra.="uploads|";
										}
										
										if (strpos($filename,"likes")) {
											$item[$indx]->extra.="likes|";
										}

									} else $continueParsing=false;
									
								}
								break;
							}
						}
						
						//print $item[$guid]["title"]." : <img src='".$item[$guid]["media"]."'>";
					}
					set_time_limit(30);
				}
				
		}  
		
		insertEntries($item,"twelveseconds",$serviceid);
	}
?>